#include "Inf_BLE.h"

#define UART_RX_BUFFER_SIZE 256
uint8_t UART_RX_BUFFER[UART_RX_BUFFER_SIZE];
uint16_t Receive_Len = 0;

void Inf_BLE_Init(void)
{
    HAL_UART_Transmit(&huart2, (uint8_t*)"AT+NAMEVergil\r\n", 15, 1000);
    HAL_Delay(10);
    HAL_UART_Transmit(&huart2, (uint8_t*)"AT+PIN1234\r\n", 12, 1000);
    HAL_Delay(10);
    HAL_UART_Transmit(&huart2, (uint8_t*)"AT+RESET\r\n", 10, 1000);
    HAL_UARTEx_ReceiveToIdle_IT(&huart2, UART_RX_BUFFER, UART_RX_BUFFER_SIZE);
    __HAL_UART_ENABLE_IT(&huart2, UART_IT_IDLE);
}

void Inf_BLE_Receive(uint8_t *data, uint16_t *len)
{
    if(Receive_Len)
    {
        *len = Receive_Len;
        Receive_Len = 0;
        memcpy(data, UART_RX_BUFFER, *len);
    }
}

void HAL_UARTEx_RxEventCallback(UART_HandleTypeDef *huart, uint16_t Size)
{
    if(huart->Instance == USART2)
    {
        Receive_Len = Size;
        HAL_UARTEx_ReceiveToIdle_IT(&huart2, UART_RX_BUFFER, UART_RX_BUFFER_SIZE);
    }
}
